package com.richpay.basejava.base16;

import java.io.ByteArrayOutputStream;

public class RSAUtils {
    private static String hexString = "0123456789ABCDEF";

    public static void main(String[] args) {
        System.out.println(encode("中文"));
        //  System.out.println(decode(encode("中文")));

        //String s = "43303135BBB6D3ADCAB9D3C3504F53CBA2BFA843313039504F53C7A9B9BAB5A543323130464646464646464646464333303331313143343132B8D0D0BBC4E3B5C4BBDDB9CB433531323430302D3136352D31363535503130313150323031315033303131503430313150353031314E30303831323334353637385431303831303030303233355432313531313233333031373031313030303654333236BABCD6DDD3E0BABCC7F8C1BCE4BED0C2C1AABACFB4F3BEC6B5EA54343135383239333331373730313130353138";
        //String s="BABCD6DDCBC4BCBEC7E0B8F6CCE5BBA7D5C5BFA1D1F4B7FECACEC9CCD0D0";
        //String s="3546313230333238464238434534344544304634444641383943313733433742413043433846363546303230355758504159";
        // String s="35463033363230303332303546303230335A464235463034363230303332303546313230333246434645464236344243374334423139414438413332353036463432384234323546313531343230323030333230313232373436";
        // String w = "46444846313131323120202020202020202020202020202032303139303823";
        //String s="43303135BBB6D3ADCAB9D3C3504F53CBA2BFA843313039504F53C7A9B9BAB5A543323130464646464646464646464333303331313143343132B8D0D0BBC4E3B5C4BBDDB9CB433531323430302D3136352D31363535503130313150323031315033303131503430313150353031314E30303831323334353637385431303831303030303036395432313531313933333139343733333030303054333036C1F5CFE8D3EE54353031314E313132303030303030303030303130";
        //String s = "35463033363230313230373546303230335A464235463034363230313230373546313531343230323031323037303435353237";

        //String s = "43303135BBB6D3ADCAB9D3C3504F53CBA2BFA843313039504F53C7A9B9BAB5A543323130464646464646464646464333303331313143343132B8D0D0BBC4E3B5C4BBDDB9CB433531323430302D3730302D38303130503130313150323031315033303131503430313150353031314E30303831323334353637385431303831303034313837345432313531333433333139353231313030303054333036D5C5BFA1D1F454353031304E313132303030303030303030303031";
        String s = "43303135BBB6D3ADCAB9D3C3504F53CBA2BFA843313039504F53C7A9B9BAB5A543323130464646464646464646464333303331313143343132B8D0D0BBC4E3B5C4BBDDB9CB433531323430302D3730302D38303130503130313150323031315033303131503430313150353031314E30303831323334353637385431303831303038323836325432313531353831343133353231313030303054333036D0EDB4BAD1DE543530322D314E313132303030303030303030303030";
        System.out.println(decode(s));

        //C015欢迎使用POS刷卡C109POS签购单C210FFFFFFFFFFC303111C412感谢你的惠顾C512400-165-1655P1011P2011P3011P4011P5011N00812345678T10810000235T215112330170良渚新联合大酒店T415829331770110518
        //C015欢迎使用POS刷卡C109POS签购单C210FFFFFFFFFFC303111C412感谢你的惠顾C512400-165-1655P1011P2011P3011P4011P5011N00812345678T10810019478T215134581753011 N112000000000001
    }
    /**
     * E4B8ADE69687
     * 5F0361433195F0203ZFB5F0462101045F120323770FA7CED9C420690274BB32165EA96
     */
    /**
     * 将字符串编码成16进制数字,适用于所有字符（包括中文）
     */
    public static String encode(String str) {
        // 根据默认编码获取字节数组
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        // 将字节数组中每个字节拆解成2位16进制整数
        for (int i = 0; i < bytes.length; i++) {
            sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
            sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
        }
        return sb.toString();
    }


    /**
     * 将16进制数字解码成字符串,适用于所有字符（包括中文）
     */
    public static String decode(String bytes) {
        String hexString = "0123456789ABCDEF";
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
        //将每2位16进制整数组装成一个字节
        for (int i = 0; i < bytes.length(); i += 2)
            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
        String bb = "";
        try {
            bb = new String(baos.toByteArray(), "GBK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bb;
    }
}
